<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <style>

    </style>

</head>
<body>
<form class="page-search layui-form" lay-filter="search-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input name="search-key" placeholder="请输入姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="search-submit">查询</button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn create-btn">新建</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="table" lay-filter="table"></table>
<div id="pagination"></div>

</body>

<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-xs" lay-event="update">更新</button>
    {{#  if(d.role.role != "主任"){ }}
    <button class="layui-btn layui-btn-xs layui-btn-danger ares-mr" lay-event="modify">重置密码</button>
    {{#  if(d.profile_info.disable){ }}
    <input type="checkbox" name="status" lay-skin="switch" data-medical-id={{d.profile_info.id}} lay-filter="status">
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" checked data-medical-id={{d.profile_info.id}} lay-filter="status">
    {{#  } }}
    {{#  } }}
</script>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['table', 'form', 'laypage', 'conf', 'utils', 'filters', 'mandyServices', 'store'], function (table, form, laypage, config, utils, filters, mandyServices, store) {
        var profile = store.get('profile'),
            $createBtn = $('.create-btn');

        utils.aresPageEventInit('updatePersonList', 'person', function () {
            getPageList(1, config.pagination.limit);
        })

        getPageList(1, config.pagination.limit);
        form.on('submit(search-submit)', function (data) {
            getPageList(1, config.pagination.limit);
            return false;
        });
        //启用禁用
        form.on('switch(status)', function (data) {
            enableProfile($(data.elem).data('medicalId'), data.elem.checked).catch(function (err) {
                data.elem.checked = !data.elem.checked;
                form.render();
            })
        });

        $createBtn.on('click', function () {
            showPersonModal('新建人员')
            return false;
        })

        function showPersonModal(title, id) {
            var queryStr = (typeof id == 'string' || typeof id == 'number') ? '?id=' + id : '';
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/system/person_form.html' + queryStr,
                end: function (index, layero) {
                    utils.ARES.cache.person = null;
                }
            });
        }

        function handlerUpdate(data) {
            utils.ARES.cache.person = data;
            showPersonModal('更新人员', data.id);
        }

        function getPageList(currentPage, pageSize) {
            if (profile.institution.role.role != "主任") return;
            var searchKey = $('input[name="search-key"]').val();
            mandyServices.mandy_get_medical_list_by_page(searchKey, currentPage, pageSize, "").then(function (res) {
                var data = res.data;
                laypage.render($.extend(config.pagination, {
                    count: data.count,
                    curr: data.current_page,
                    jump: function (obj, first) {
                        if (!first) getPageList(obj.curr, obj.limit);
                    }
                }));
                table.render({
                    elem: '#table',
                    data: data.list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '姓名', templet: '<span>{{d.profile_info.name}}</span>'},
                        {
                            title: '性别',
                            width: 50,
                            templet: function (d) {
                                return filters.sexFormat(d.profile_info.sex);
                            }
                        },
                        {title: '手机', field: 'mobile', width: 90},
                        {title: '医院', templet: '<span>{{d.hospital.name}}</span>', width: 220},
                        {title: '科室', templet: '<span>{{d.division.name}}</span>'},
                        {
                            title: '岗位',
                            templet: function (d) {
                                if (d.job == "therapist") {
                                    return "治疗师";
                                } else if (d.job == "doctor") {
                                    return "医生";
                                }
                            }
                        },
                        {
                            title: '分组',
                            templet: function (d) {
                                if (d.job == "doctor") {
                                    return "医生组";
                                } else {
                                    if (d.treatment_group != null) {
                                        return d.treatment_group.name;
                                    } else {
                                        return "-";
                                    }
                                }
                            }
                        },
                        {title: '角色', templet: '<span>{{d.role.role}}</span>'},
                        {title: '操作', fixed: 'right', width: 180, templet: '#tableBar'}
                    ]]
                });
                table.on('tool(table)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'update':
                            handlerUpdate(data)
                            break;
                        case 'modify':
                            handlerModifyPwd(data)
                            break;
                    }
                    ;
                });
            });
        }

        function enableProfile(medicalId, checked) {
            return mandyServices.mandy_put_medical_disabled(medicalId, !checked).then(function (res) {
                utils.msgSuccess(res.message);
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/12 16:20
         * @description: 重置密码
         * @param{object} data  当前人员信息
         */
        function handlerModifyPwd(data) {
            utils.confirm('人员 "' + data.profile_info.name + '" 的密码将被重置为默认密码(用户生日), 是否继续?', {icon: 3, title: '重置密码'}, function (index) {
                    mandyServices.mandy_put_medical_pwd(data.profile_info.id, data.birthday.replaceAll("-", "")).then(function (res) {
                        utils.msgSuccess(res.message);
                        utils.layer.close(index);
                    })
                }
            );
        }
    })
</script>


</html>